package com.xcc;

import javazoom.jl.player.advanced.AdvancedPlayer;

import java.io.InputStream;

public class XccPlayer extends Thread {
    private String fileName;
    private AdvancedPlayer player;

    public static XccPlayer of(String filename) {
        return new XccPlayer().setFileName(filename).initPlayer();
    }
    /*public static com.xcc.XccPlayer of(String filename) {
        return new com.xcc.XccPlayer().setFileName(filename).initPlayer();
    }*/

    private XccPlayer() {
    }

    private XccPlayer setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    private XccPlayer initPlayer() {
        try {
            /*BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(fileName));
            player = new Player(buffer);*/
            InputStream inStream = XccPlayer.class.getClassLoader()
                    .getResourceAsStream(fileName);
            player = new AdvancedPlayer(inStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    @Override
    public void run() {
        try {
            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
        player = null;
    }
}
